package com.lzy.business.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.product.apis.*;
import com.lzy.product.dtos.ConditionDto;
import com.lzy.product.dtos.ProductClassifyDto;
import com.lzy.product.dtos.ProductSaleConditionDto;
import com.lzy.product.dtos.ProductSaleLevelDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by WIN7 on 2017/12/12.
 */
@Component
public class ProductSaleLevelFacade {

    @Autowired
    ProductSaleLevelServiceApi productSaleLevelServiceApi;

    @Autowired
    ProductClassifyServiceApi productClassifyServiceApi;

    @Autowired
    ConditionServiceApi conditionServiceApi;

    @Autowired
    ProductSaleConditionServiceApi productSaleConditionServiceApi;

    /**
     * 查询开通下级列表
     */
    public PageInfo<ProductSaleLevelDto> getList(Pager pager,ProductSaleLevelDto dto){
        //查询开通下级列表
        PageInfo<ProductSaleLevelDto> pageInfo = productSaleLevelServiceApi.selectList(pager,dto);
        if(pageInfo != null && pageInfo.getList() != null && pageInfo.getSize() > 0) {
            List<ProductSaleLevelDto> productSaleLevelDtoList = pageInfo.getList();
            for (ProductSaleLevelDto pslDto : productSaleLevelDtoList) {
                if (pslDto != null) {
                    if(pslDto.getClassifyId() != null) {
                        ProductClassifyDto productClassifyDto = productClassifyServiceApi.findById(pslDto.getClassifyId());
                        if(productClassifyDto != null && productClassifyDto.getName() != null){
                            pslDto.setClassifyName(productClassifyDto.getName());
                        }
                    }
                    if(pslDto.getConditionId() != null){
                        ConditionDto conditionDto = conditionServiceApi.findById(pslDto.getConditionId());
                        if(conditionDto != null && conditionDto.getName() != null){
                            pslDto.setConditionName(conditionDto.getName());
                        }
                    }
                }
            }
        }
        return pageInfo;
    }

    /**
     * 批量移除
     */
    public Boolean removeList(Long[] idList){
        String ids = "";
        Integer n = 0;
        for (Long id:idList) {
            ids=ids + id + ",";
            n++;
        }
        ids = ids.substring(0,ids.length()-1);
        Integer m = productSaleLevelServiceApi.updateList(ids);
        Ensure.that(m.equals(n)).isTrue("01000009");
        return true;
    }

    /**
     * 获取列表信息跳转至新增页面
     */
    public ProductSaleLevelDto toAdd(){
        ProductSaleLevelDto productSaleLevelDto = new ProductSaleLevelDto();
        //获取分类列表
        List<ProductClassifyDto> classifyDtoList = productClassifyServiceApi.query(null);
        if(classifyDtoList != null){
            productSaleLevelDto.setClassifyList(classifyDtoList);
        }
        //获取条件列表
        List<ConditionDto> conditionDtoList = conditionServiceApi.query(null);
        if(conditionDtoList != null){
            productSaleLevelDto.setConditionList(conditionDtoList);
        }
        return productSaleLevelDto;
    }

    /**
     * 开通条件新增
     */
    public Boolean insert(ProductSaleLevelDto dto){
        //查询该分类下是否有增员标准（一个分类下面只有一个分类标准）
        List<ProductSaleLevelDto> saleLevelDtoList = productSaleLevelServiceApi.query(dto);
        Ensure.that(saleLevelDtoList).isNotNull("06000005");
        Long b = productSaleLevelServiceApi.insert(dto);
        Ensure.that(b).isNotNull("06000003");
        //向开通下级与条件关联表添加关联关系
        if (dto.getNumberArr() != null){
            ProductSaleConditionDto productSaleConditionDto = new ProductSaleConditionDto();
            productSaleConditionDto.setSaleId(b);
            productSaleConditionDto.setConditionId(dto.getConditionId());
            String stages = "";
            for (String l : dto.getNumberArr()){
                if(l != null && !l.equals(" ")){
                    stages = stages + l + ",";
                }
            }
            productSaleConditionDto.setStages(stages.substring(0,stages.length()-1));
            Long i = productSaleConditionServiceApi.insert(productSaleConditionDto);
            if(!i.equals(null)){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取信息跳转至编辑页面
     */
    public ProductSaleLevelDto toEdit(Long id){
        ProductSaleLevelDto productSaleLevelDto = productSaleLevelServiceApi.findById(id);
        if(productSaleLevelDto != null) {
            //获取分类名称
            if(productSaleLevelDto.getClassifyId() != null) {
                ProductClassifyDto pcDto = productClassifyServiceApi.findById(productSaleLevelDto.getClassifyId());
                if(pcDto != null && pcDto.getName() != null){
                    productSaleLevelDto.setClassifyName(pcDto.getName());
                }
            }

            //获取分类列表
            List<ProductClassifyDto> classifyDtoList = productClassifyServiceApi.query(null);
            if (classifyDtoList != null) {
                productSaleLevelDto.setClassifyList(classifyDtoList);
            }
            //获取条件列表
            List<ConditionDto> conditionDtoList = conditionServiceApi.query(null);
            if (conditionDtoList != null) {
                productSaleLevelDto.setConditionList(conditionDtoList);
            }
            //获取关联表的阶段条件
            ProductSaleConditionDto pscDto = new ProductSaleConditionDto();
            pscDto.setSaleId(id);
            pscDto.setConditionId(productSaleLevelDto.getConditionId());
            List<ProductSaleConditionDto> productSaleConditionDtoList = productSaleConditionServiceApi.query(pscDto);
            if(productSaleConditionDtoList != null && productSaleConditionDtoList.size() ==1){
                String stages = productSaleConditionDtoList.get(0).getStages();
                String[] strArray = null;
                strArray = stages.split(",");
                String[] numberArr = new String[strArray.length];
                for(int r = 0;r<strArray.length;r++){
                    if(strArray[r] != null){
                        numberArr[r] = strArray[r];
                    }
                }
                productSaleLevelDto.setNumberArr(numberArr);
            }
            return productSaleLevelDto;
        }
        return null;
    }

    /**
     * 更新条件
     */
    public Boolean update(ProductSaleLevelDto dto){
        //更新开通条件
        ProductSaleLevelDto productSaleLevelDto = new ProductSaleLevelDto();
        productSaleLevelDto.setId(dto.getId());
        productSaleLevelDto.setClassifyId(dto.getClassifyId());
        productSaleLevelDto.setConditionId(dto.getConditionId());
        Boolean b = productSaleLevelServiceApi.update(productSaleLevelDto);
        if(b) {
            //先删除关联表信息
            ProductSaleConditionDto saleConditionDto = new ProductSaleConditionDto();
            saleConditionDto.setSaleId(dto.getId());
            Integer k = productSaleConditionServiceApi.deleteByPrimaryKey(saleConditionDto);
            Ensure.that(k.equals(0)).isFalse("06000004");
            //新增关联表信息
            ProductSaleConditionDto productSaleConditionDto = new ProductSaleConditionDto();
            productSaleConditionDto.setSaleId(dto.getId());
            productSaleConditionDto.setConditionId(dto.getConditionId());
            if (dto.getNumberArr() != null) {
                String stages = "";
                for (String l : dto.getNumberArr()) {
                    if (l != null && !l.equals(" ")) {
                        stages = stages + l + ",";
                    }
                }
                productSaleConditionDto.setStages(stages.substring(0, stages.length() - 1));
            }
            Long i = productSaleConditionServiceApi.insert(productSaleConditionDto);
            if(!i.equals(null)){
                return true;
            }
        }
        return false;
    }
}
